﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FoneBuddy.LinqData;
using FoneBuddy.LinqModel.Data;
using FoneBuddy.LinqModel;

public partial class MerchantLogin : System.Web.UI.Page
{
    IDataRepository _dataObject = new DataRepository();

    protected void Page_Load(object sender, EventArgs e)
    {
        //Page.ClientScript.RegisterStartupScript(GetType(), "MyKey", "callAlert();", true);
       
    }
    protected void OnLogin(object sender, EventArgs e)
    {
        Merchant merchant = new Merchant();
        merchant = new Merchant(_dataObject);
        merchant.Email = txtUserName.Text;
        merchant.GetMerchantByEmailId();
        if (merchant.Id > 0)
        {
            SubscriptionHistory subHistory = new SubscriptionHistory(_dataObject);
            List<SubscriptionHistory> subHistories = new List<SubscriptionHistory>();
            subHistory.MerchantId = merchant.Id;
            subHistories = subHistory.GetSubscriptionHistoryByMerchantId();

            if (merchant.Password.Equals(txtPassword.Text) && merchant.IsAdmin != true && subHistories.ElementAt(0).IsSuccess == true && subHistories.ElementAt(0).IsActive == true && subHistories.ElementAt(0).ExpiryDate >= DateTime.Today)
            {
                merchant.UpdateLoginInfo();
                Response.Redirect("~/default.aspx");
            }
            else
            {
                divMessage.InnerHtml = "<strong>Error!</strong> Invalid Username or password.";
                divMessage.Attributes.Add("class", "alert alert-danger");
                return;
            }

        }
        else
        {
            divMessage.InnerHtml = "<strong>Error!</strong> Invalid Username or password.";
            divMessage.Attributes.Add("class", "alert alert-danger");
            return;
        }


    }

    [System.Web.Services.WebMethod]
    public static string[] checkMail(string emailid, string password)
    {
        //loginDetail[0] means it returns user is expired or not if it is expire then it returns true else it returns false 
        //liginDetails[1] returns Merchant id if it is exits in syatem...
        string s = emailid;
        IDataRepository _dataObject = new DataRepository();
        Merchant merchant = new Merchant();
        merchant = new Merchant(_dataObject);
        merchant.Email = emailid;
        merchant.Password = password;
        merchant.IsAdmin = false;
        string[] loginDetail = new string[2];
        loginDetail = merchant.IsValidLogin();
       
        if(loginDetail[1]!="0")
        {
            merchant.Id=Convert.ToInt32(loginDetail[1]);
            merchant.GetMerchantById();
            SessionWrapper.Merchant = merchant;
        }
        return loginDetail;
    }
    protected void OnForgotPassword(object sender, EventArgs e)
    {
        IDataRepository _dataObject = new DataRepository();
        Merchant merchant = new Merchant();
        merchant = new Merchant(_dataObject);
        merchant.Email = txtOldEmailId.Text;
        merchant.GetMerchantByEmailId();
        if (merchant.Id > 0)
        {
            merchant.Password = EncryptionDecryption.Decrypt(merchant.Password);
            string s = "";
            s = s + "<div style='width:120px;float:left;'>Your EmailId : </div>" + merchant.Email + "<br /><br />";
            s = s + "<div style='width:120px;float:left;'>Your Password : </div>" + "<strong> " + merchant.Password + "</strong>";

            bool i = MailMessage.SendMessage("chandnijain191@gmail.com", "Password", s);
            if (i == false)
            {
                divMessage.InnerHtml = "<strong>Error!</strong> Password Sent Successfully.";
                divMessage.Attributes.Add("class", "alert alert-success");
                return;
            }
        }
    }
    protected void OnUpgrade(object sender, EventArgs e)
    {

        EncryptionDecryption.Decrypt("");
        Response.Redirect("~/MerchantSubscription.aspx");
    }
}